﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using Skybot.Arcgis.Client.TileLayerExpand;
using System.Net;

namespace 地图拼接平移工具
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {

            InitializeComponent();
        }



        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //http://localhost/tiles/r1321211021120322.png?g=49
            System.Net.WebClient wc = new System.Net.WebClient();


            var r1321211021120322 = new System.Drawing.Bitmap(new System.IO.MemoryStream(wc.DownloadData("http://localhost/tiles/r1321211021120322.png?g=49")));
            r1321211021120322 = r1321211021120322.Move(50, 50);

            var sy = r1321211021120322.ConvertBitmap(BitmapManipulator.MIME_PNG);
            sy.Save(@"C:\Users\sky\Desktop\a.png");


            #region 以下实现图片拼接

            #region 計算圖片左右兩個頂點的坐標位置
            ///圖片的高寬
            int PicWidthHeight = 256;

            System.Windows.Point 左上頂點 = new System.Windows.Point();
            System.Windows.Point 右下角點 = new System.Windows.Point();


            #region 計算當前Mouse移動在地圖中所在的px
            System.Windows.Point 左上頂點px = new System.Windows.Point();
            System.Windows.Point 右下頂點px = new System.Windows.Point();
            #endregion

            var x = 0;
            var y = 0;
            var z = 0;
            TileSystem.QuadKeyToTileXY("132121102112032", out x, out y, out z);
            string Quadkey = TileSystem.TileXYToQuadKey(x, y, z);



            #endregion

            new System.Threading.Thread(new System.Threading.ThreadStart(delegate
            {
                //这里暂不用到 主要功能是实现地图图片的拼接
                //来源 http://social.microsoft.com/Forums/id-ID/visualcshartzhchs/thread/149abb28-8ba6-45b7-92e7-cc19ede3a4d3
                int iTopLeftX = x;
                int iTopLeftY = y;

                //最多一次可以处理89*89个256*256的图片
                int iBottomRightX = iTopLeftX + 2;
                int iBottomRightY = iTopLeftY + 2;

                int iRowNum = iBottomRightY - iTopLeftY + 1;
                int iColumnNum = iBottomRightX - iTopLeftX + 1;
                double Current = 0.0;

                Dispatcher.BeginInvoke(new Action(delegate {  progressBar1.Value = Current / (iRowNum * iColumnNum);}));


                System.Drawing.Bitmap img = new Bitmap(256 * iRowNum, 256 * iColumnNum);
                Graphics graph = Graphics.FromImage(img);
                for (int i = 0; i < iColumnNum; i++)
                {
                    for (int j = 0; j < iRowNum; j++)
                    {
                        try
                        {
                            Bitmap bm = new System.Drawing.Bitmap(new System.IO.MemoryStream(wc.DownloadData("http://localhost/tiles/r" + TileSystem.TileXYToQuadKey(i + iTopLeftX, j + iTopLeftY, z) + ".png?g=49")));
                            graph.DrawImage(bm, i * 256, j * 256);
                        }
                        catch (WebException)
                        {

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("失败!");
                        }
                        Current++;
                        Dispatcher.BeginInvoke(new Action(delegate { progressBar1.Value = Current / (iRowNum * iColumnNum)*100; })); 
                    }
                }
                graph.Dispose();
                img = img.Clone(new System.Drawing.Rectangle(107, 210, img.Height - 107, img.Width - 210), img.PixelFormat);
                img.ConvertBitmap(BitmapManipulator.MIME_PNG).Save(@"C:\Users\sky\Desktop\b.png");
            })).Start();


            #endregion
        }


    }
}
